﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member.UserControl
{
    public partial class SamPhamChiTiet : System.Web.UI.UserControl
    {
        private List<CommentDTO> comments;
        public static int CurrentPage = 0;
        private int countPage = 0;
        private string ma = null;
        private List<SanPhamDTO> tb = new List<SanPhamDTO>();
        //List<TimeSpan> timeSpans = new List<TimeSpan>();
        private double dtime = 86400;
        double totalSeconds = 0;
        int seconds = 0;
        int minutes = 0;
        private int hours = 0;
        string time = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            ma = Request.QueryString["id"];

            if (!IsPostBack)
            {
                comments = CommentBUS.GetCommentOfSP(ma);
                CurrentPage = 0;
                ViewState["CommentList"] = comments;
                DataBindDataList();
                DataBindDataListProductsByCategory();


                tb = SanPhamBUS.getTopByDate(5);
                ViewState["Time"] = dtime;
                DataListSanPham.DataSource = tb;
                DataListSanPham.DataBind();
            }
            else
            {
                dtime = (double)ViewState["Time"];
                comments = (List<CommentDTO>)ViewState["CommentList"];
            }


        }
        protected void ImageButton1_Click(object sender, EventArgs e)
        {
            //String maSamPham = Request.QueryString["id"].ToString().Trim();
            //Response.Redirect("~/Member/ChiTietGioHang.aspx?idsanpham=" + maSamPham);
        }
       

        protected void btSend_Click(object sender, EventArgs e)
        {

            if (Session["username"] != null)
            {
                if (String.IsNullOrEmpty(txtContent.Text))
                {
                    return;
                }
                Random random = new Random();
                int maComment = random.Next(1000000, 99999999);
                CommentDTO commentDto = new CommentDTO();
                commentDto.MaCommment = maComment;
                commentDto.TenTaiKhoan = Session["username"].ToString().Trim();
                commentDto.MaSanPham = ma;
                commentDto.NoiDungComment = txtContent.Text.Trim();
                commentDto.NgayComment = DateTime.Now;
                TaiKhoanDTO taiKhoanDto = TaiKhoanBUS.GetTaiKhoanOfTen(commentDto.TenTaiKhoan);
                commentDto.MaTaiKhoan = taiKhoanDto.MaTaiKhoan;
                commentDto.Avatar = taiKhoanDto.Avatar;
                bool res = CommentBUS.InsertComment(commentDto);
                if (res)
                {
                    comments.Add(commentDto);
                    ViewState["CommentList"] = comments;
                    DataBindDataList();
                    DataBindDataListProductsByCategory();
                }

            }
            else
            {
                //Session.Add("previousUri", Request.RawUrl);
                Response.Redirect("/Guest/LoginPage.aspx");
            }



        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);

            if (Session["username"] != null)
            {
                Response.Redirect("~/Member/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (pgns - 1));
            }
            else
            {
                Response.Redirect("~/Guest/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (pgns - 1));
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);
            if (Session["username"] != null)
            {
                Response.Redirect("~/Member/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (pgns + 1));
            }
            else
            {
                Response.Redirect("~/Guest/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (pgns + 1));
            }
        }

        private void DataBindDataList()
        {
            //string ma = Request.QueryString["id"];
            // phan trang
            countPage = comments.Count / 3;
            if (comments.Count % 3 != 0)
            {
                countPage = comments.Count / 3 + 1;
            }


            for (int i = 0; i < countPage; i++)
            {
                HyperLink hlNumberPages = new HyperLink();
                LiteralControl literal = new LiteralControl("&nbsp;&nbsp;");
                hlNumberPages.Text = (i + 1).ToString();
                if (Session["username"] != null)
                {
                    hlNumberPages.NavigateUrl = string.Format("~/Guest/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (i + 1));
                }
                else
                {
                    hlNumberPages.NavigateUrl = string.Format("~/Guest/ChiTietSanPham.aspx?id=" + ma + "&pgn=" + (i + 1));
                }

                Panel2.Controls.Add(hlNumberPages);
                Panel2.Controls.Add(literal);

            }
        }


        private void DataBindDataListProductsByCategory()
        {
            string page = Request.QueryString["pgn"];
            int pgn = 1;
            Int32.TryParse(page, out pgn);

            List<CommentDTO> sanphamDTO = GetSanPhamListForPage(pgn - 1, 3, comments);
            if (sanphamDTO.Count > 0)
            {
                ViewState["CommentList"] = comments;
                DataList1.DataSource = sanphamDTO;
                DataList1.DataBind();


                //Hien Prev hoac Next button neu nhu CurrentPage o trang dau hoac cuoi
                if (pgn == 1)
                {
                    lbTruoc.Visible = false;
                }
                else
                {
                    lbTruoc.Visible = true;
                }
                if (pgn == countPage)
                {
                    lbSau.Visible = false;
                }
                else
                {
                    lbSau.Visible = true;
                }

            }
            else
            {
                lbSau.Visible = false;
                lbTruoc.Visible = false;
            }

        }

        private List<CommentDTO> GetSanPhamListForPage(int pageIdx, int pageSize, List<CommentDTO> srcList)
        {
            int start = pageIdx * pageSize;
            int end = start + pageSize;
            if (end > srcList.Count)
            {
                end = srcList.Count;
            }

            List<CommentDTO> slicedList = new List<CommentDTO>();
            for (int i = start; i < end; i++)
            {
                slicedList.Add(srcList[i]);
            }
            return slicedList;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            ViewState["Time"] = Convert.ToDouble(ViewState["Time"]) - 1;
            if (Convert.ToDouble(ViewState["Time"]) <= 0)
            {

            }
            else
            {
                totalSeconds = Convert.ToDouble(ViewState["Time"]);
                int temp = (int)totalSeconds / 60;
                seconds = (int)totalSeconds % 60;
                hours = temp / 60;
                minutes = temp % 60;

                time = hours + ":" + minutes + ":" + seconds;

                for (int i = 0; i < DataListSanPham.Items.Count; i++)
                {
                    Label label = (Label)DataListSanPham.Items[i].FindControl("Label1");
                    label.Text = time;

                }
            }
        }


    }
}